import Vue from 'vue'
import Router from 'vue-router'
import Home from './home'
import Asset from './asset'
import Trade from './trade'
import Mine from './mine'
import vue from '../main.js'

const originalPush = Router.prototype.push
Router.prototype.push = function push(location, resolve, reject) {
  if (resolve || reject) return originalPush.call(this, location, resolve, reject)
  return originalPush.call(this, location).catch((e) => { })
}
const SignIn = () => import('@/views/SignIn/index.vue');
const Register = () => import('@/views/Register/index.vue')
const Update = () => import('@/views/Update/index.vue')
const Markets = () => import('@/views/Markets/index.vue')
const Customerservice = () => import('@/views/Customerservice/index.vue')
const Lang = () => import('@/views/LangSwitch/index.vue')
const Line = () => import('@/views/LineSwitch/index.vue')
const Download = () => import('@/views/Download/index.vue')

Vue.use(Router)

const router = new Router({
  routes: [
    ...Home,
    ...Asset,
    ...Trade,
    ...Mine,
    {
      path: '/',
      redirect: {
        name: 'home',
      }
    },
    {
      path: '/markets',
      name: 'markets',
      component: Markets
    },
    {
      path: '/mine',
      component: Mine,
      redirect: {
        name: 'mine',
      }
    },
    {
      path: '/signIn',
      name: 'signIn',
      component: SignIn
    },
    {
      path: '/register',
      name: 'register',
      component: Register
    },
    {
      path: '/update',
      name: 'update',
      component: Update
    },
    {
      path: '/customerservice',
      name: 'customerservice',
      component: Customerservice
    },
    {
      path: '/lang',
      name: 'lang',
      component: Lang
    },
    {
      path: '/line',
      name: 'line',
      component: Line
    },
    {
      path: '/download',
      name: 'download',
      component: Download
    }
  ]
})
router.beforeEach((to, from, next) => {
  let titleName = localStorage.getItem('titleName')
  if (titleName === null) {
    document.title = 'Exchange'
  } else {
    document.title = titleName;
  }
  let userInfo = localStorage.getItem('userInfo')
  let routerList = ['/home', '/markets', '/signIn', '/register', '/update', '/download', '/line', '/lang', '/customerservice', '/trade','/message']
  if (routerList.includes(to.path)) {
    next()
  } else if (userInfo === null) {
    vue.$dialog.confirm({
      title: vue.$t('info.info20'),
      confirmButtonText: vue.$t('info.info21'),
      cancelButtonText: vue.$t('info.info11'),
      confirmButtonColor: 'rgb(218, 171, 81)',
      cancelButtonColor: 'rgb(218, 171, 81)',
      theme: 'round-button'
    }).then(() => {
      next('/signIn')
    }).catch(() => { })
  } else {
    next()
  }
})

export default router
